package com.ghsc.yigou.live.ui.login

import android.text.TextUtils
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.config.AppConfig
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.http.constant.HttpConstant
import com.cn.appcore.utils.Preference
import com.ghsc.yigou.live.R
import com.ghsc.yigou.live.config.UserManager
import com.ghsc.yigou.live.databinding.ActivityLoginBinding
import com.ghsc.yigou.live.ui.MainActivity

/**
 * 项目：YiGouLive
 * 包名：com.ghsc.yigou.live.ui.login
 * 类名：LoginActivity
 * 创建时间：2024/8/1 10:19
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class LoginActivity  : BaseVMActivity<LoginViewModel, ActivityLoginBinding>()  {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_login
    }

    override fun attachVMClass(): Class<LoginViewModel>? {
        return  LoginViewModel::class.java
    }

    override fun initView() {

    }


    override fun initClickListener() {
        super.initClickListener()
        binding.btnLogin.setSingleClickListener {
            val isChecked = binding.cbUserAgreement.isChecked
            if (!isChecked) {
                showToast("请阅读并同意《用户服务协议及隐私政策》")
                binding.cbUserAgreement.isChecked=true
                return@setSingleClickListener
            }
            val  phoneStr = binding.etPhone.text.toString().trim()
            val loginPwd = binding.etLoginPwd.text.toString().trim()
            if (TextUtils.isEmpty(phoneStr)){
                showToast("请输入手机号")
                return@setSingleClickListener
            }
            if (phoneStr.length != 11) {
                showToast("手机格式错误")
                return@setSingleClickListener
            }
            if (TextUtils.isEmpty(loginPwd)){
                showToast("请输入密码")
                return@setSingleClickListener
            }
            if (loginPwd.length < 6) {
                showToast("密码错误，不得少于6位")
                return@setSingleClickListener
            }


            var userId: String by Preference(HttpConstant.USER_ID, "")
            var userRid: String by Preference(HttpConstant.USER_RID, "")
            userId =phoneStr
            userRid=phoneStr
            if (!TextUtils.isEmpty(userId)) {
                UserManager.setUserId(phoneStr)
                UserManager.setUserName("官方直播")
                UserManager.setUserHead("")
                showToast("登录成功")
                var isLogin: String by Preference(HttpConstant.APP_IS_LOGIN, "")
                isLogin = "1"
                startActivity(MainActivity().javaClass)
                //除了登录activity不关闭。其余的都关闭。
                AppConfig.getActivityManager()
                    ?.popAllActivityExceptOne(MainActivity::class.java)
            }


        }
    }


}